﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Views;
using CommunitySystem.Assert;
using CommunitySystem.Ctrls;
using CommunitySystem.JinShengService;
using EHR.Common;
using System.Collections.ObjectModel;
using System.Windows.Data;

namespace CommunitySystem.PageView
{
    public partial class SysInfoIndexControl : UserControl
    {
        private readonly JinShengServiceClient _client = new JinShengServiceClient();
        private DataService.MenuItem _menu;
        public SysInfoIndexControl()
        {
            InitializeComponent();


            if (Session.LoginUser.RoleID == 2)//人事干部
                _client.GetSystemInfoByCountAsync(string.Format(@" and CreateUserID=2 
            and CHARINDEX(CONVERT(varchar(100), OrgIDs),(select OrgCode from UserInfo where UserID={0}))>0", Session.LoginUser.UserID));
            else if (Session.LoginUser.RoleID == 1) //管理员
                _client.GetSystemInfoByCountAsync("");
            _client.GetSystemInfoByCountCompleted += new EventHandler<GetSystemInfoByCountCompletedEventArgs>(_client_GetSystemInfoByCountCompleted);

            _client.GetSystemInfoCompleted += new EventHandler<GetSystemInfoCompletedEventArgs>(_client_GetSystemInfoCompleted);

           
        }

        void _client_GetSystemInfoCompleted(object sender, GetSystemInfoCompletedEventArgs e)
        {
            dg.ItemsSource = null;
            dg.ItemsSource = e.Result;

            IsWorking.Visibility = Visibility.Collapsed;
        }
        private int pageSize = 20;
        void _client_GetSystemInfoByCountCompleted(object sender, GetSystemInfoByCountCompletedEventArgs e)
        {
            ObservableCollection<int> list = new ObservableCollection<int>();
            int last = e.Result % pageSize;
            int count = 0;
            if (last > 0)
                count = e.Result / pageSize + 1;
            else
                count = e.Result / pageSize;
            for (int i = 0; i < count; i++)
            {
                list.Add(i);
            }

            PagedCollectionView pcv = new PagedCollectionView(list);
            dpPageSize.PageSize = 1;
            dpPageSize.Source = null;
            dpPageSize.Source = pcv;
        }

        private void dpPageSize_PageIndexChanged(object sender, EventArgs e)
        {
            int index = dpPageSize.PageIndex == -1 ? 0 : dpPageSize.PageIndex;


            if (Session.LoginUser.RoleID == 2)//人事干部
                _client.GetSystemInfoAsync(string.Format(@" and CreateUserID=2 
            and CHARINDEX(CONVERT(varchar(100), OrgIDs),(select OrgCode from UserInfo where UserID={0}))>0", Session.LoginUser.UserID), index + 1, pageSize);
            else if (Session.LoginUser.RoleID == 1) //管理员
                _client.GetSystemInfoAsync("", index + 1, pageSize);
            
        }

   
     
        public void BeginReturn()
        {
           
        }

   
    }
}
